;
(function (window, angular) {
    'use strict';
    angular.module("Controller.Merchant.UreportManage", [])
        .controller("UreportManageCtrl", ["$scope", "ybWidget", "ybHttp", "MESG_TYPE", "$filter", "$timeout", function ($scope, ybWidget, ybHttp, MESG_TYPE, $filter, $timeout) {
            $scope.activeFlag = [true, false];
            $scope.markInfo = {
                curRow: 0
            };
            $scope.pageInfo = {
                totalPages: 1,
                totalRows: 0,
                pageSize: 10,
                curPage: 1
            };
            $scope.toolbarConfig = {
                showAdvanceSearch: false,
                showColConfig: false
            };

            $scope.condition = {};
            $scope.dataList = [];

            $scope.tableConfig = {
                fixedWidth: 110,
                scrollWidth: 600,
                tableCol: [
                    {
                        name: "文件名",
                        dataField: "reportname",
                        width: 300,
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        name: "创建时间",
                        dataField: "createDateShow",
                        width: 140,
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        name: "更新时间",
                        dataField: "modifiedDateShow",
                        width: 140,
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        name: "操作",
                        isShow: true,
                        isFixed: true,
                    }
                ]
            };

            $scope.changeTab = function (index) {
                $scope.activeFlag = [false, false];
                $scope.activeFlag[index] = true;
                $scope.submitted = false;
                switch (index) {
                    case 0:
                        break;
                    default:
                        break;
                }
            };

            $scope.search = function (curpage) {
                getDataList(curpage);
            };

            function getDataList(curPage) {
                var service = "device";
                var reqUrl = "/ureportManage/listAllPage";
                var param = {};
                if (!!curPage) {
                    $scope.pageInfo.curPage = curPage;
                }
                param.pageSize = $scope.pageInfo.pageSize;
                param.pageNum = $scope.pageInfo.curPage;
                param.filters = angular.copy($scope.condition);
                ybHttp.Post(service, reqUrl, param).then(function (resp) {
                    if (resp.code !== 0) {
                        ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                        return;
                    }
                    angular.forEach(resp.value.list, function (item) {
                        item.createDateShow = $filter("YbDate")(item.createDate, 'yyyy-MM-dd HH:mm:ss');
                        item.modifiedDateShow = $filter("YbDate")(item.modifiedDate, 'yyyy-MM-dd HH:mm:ss');
                        item.url = $scope.hostOrigin + "/dev/ureport/preview?_u=" + item.previewReportname;
                    });
                    $scope.pageInfo.totalRows = resp.value.total;
                    $scope.pageInfo.totalPages = resp.value.pages;
                    $scope.dataList = resp.value.list;
                });
            }

            $scope.setFixedWidth = function () {
                $scope.tbStyle = {scrollLeft: {left: $scope.tableConfig.fixedWidth + "px"}};
            };

            $scope.switchToolbar = function (toolbar) {
                if (!!$scope.toolbarConfig["show"+ toolbar]) {
                    $scope.toolbarConfig["show"+ toolbar] = false;
                } else {
                    $timeout(function () {
                        $scope.toolbarConfig["show"+ toolbar] = true;
                    });
                }
            };

            $scope.selectRow = function (index) {
                $scope.markInfo.curRow = index
            };

            $scope.reset = function () {
                $scope.condition = {};
            };

            $scope.addData = function () {
                $scope.changeTab(1);
            };
            $scope.ureportDesignerUrl = window.location.origin + "/dev/ureport/designer";

            function getRelateCompanys() {
                return ybHttp.Get('device', '/ureportManage/relateCompanys', { reportId: $scope.curReportId }).then(function (resp) {
                    if (resp.code !== 0) {
                        ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                        return;
                    }
                    $scope.bindCompanyList = resp.value.companys;
                });
            }

            $scope.bindCompanyList = [];
            $scope.handleBindCompany = function (val) {
                $scope.curReportId = val;
                getRelateCompanys().then(function () {
                    $scope.changeTab(2);
                })
            };
            $scope.previewItem = function (url) {
                window.open(url, '_blank');
            };

            $scope.bindCompany = function () {
                const options = {
                    title: '公司',
                    url: '/company/list',
                    params: { isDelivery: 1, outStatus: 2, isLinked: 0 },
                    keyWords: [{label: '公司名称', field: 'companyName'}],
                    fields: [
                        {
                            name: '公司编码',
                            dataField: 'orgId',
                            width: "50%"
                        },
                        {
                            name: '公司名称',
                            dataField: 'companyName',
                            width: "50%"
                        }
                    ],
                    selectedKey: 'orgId',
                    selectedIds: $scope.bindCompanyList.map(function(b) { return b.orgId }),
                }
                ybWidget.DataSelector(options).then(function (data) {
                    $scope.bindCompanyList.push(data);
                })
            };

            $scope.delCompany = function (index) {
                $scope.bindCompanyList.splice(index, 1);
            };

            $scope.saveData =function () {
                const params = {
                    reportId: $scope.curReportId,
                    orgIds: $scope.bindCompanyList.map(function(b) { return b.orgId }),
                };
                ybHttp.Post('device', '/ureportManage/relateCompanyReport', params).then(function (res) {
                    if (res.code !== 0) {
                        ybWidget.ShowMessage(res, MESG_TYPE.Error);
                        return;
                    }
                    $scope.changeTab(0);
                });
            };


            (function () {
                if (!window.location.origin) {
                    $scope.hostOrigin = window.location.protocol + '//' + window.location.host;
                } else {
                    $scope.hostOrigin = window.location.origin;
                }
                getDataList();
                $scope.setFixedWidth();
            })();
        }])
    ;
})(window, window.angular);